package com.sulent.frame.file.vfs.example;


import com.jcraft.jsch.JSch;
import org.apache.commons.vfs2.*;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @ClassName VFSExample
 * @Description: 基于Apache-Commons-VFS 实现多文件系统
 * @Author Submerge.
 * @Since 2024/1/22 14:58
 * @Version 1.0
 */
public class VFSExample {

    public static void main(String[] args) throws FileSystemException, UnsupportedEncodingException {

        FileSystemManager manager = VFS.getManager();

        //1.解析本地文件，
        // 格式：file:///somedir/some%25file.txt
        // 格式示例：file:///home/someuser/somedir
        System.out.println("解析本地文件：=======================================");
        FileObject fileObject1 = manager.resolveFile("file://D:\\Keep-Update\\Study-Way");
        FileName name = fileObject1.getName();
        System.out.println(name);
        FileObject[] children1 = fileObject1.getChildren();
        for (FileObject fileObject : children1) {
            System.out.println("名称："+fileObject.getName().getBaseName()+"---"+"类型:"+ fileObject.getType().getName());
//            System.out.println("大小："+fileObject.getContent().getSize());
        }

        manager.close();


/**
 * FTP协议
 * 提供对 FTP 服务器上的文件的访问。
 * URI 格式:ftp://[ username[: password]@] hostname[: port][ relative-path]
 * 例子:ftp://myusername:mypassword@somehost/pub/downloads/somefile.tgz
 * 默认情况下，该路径相对于用户的主目录。这可以通过以下方式进行更改：FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, false);
 */

        System.out.println("解析FTP文件：=======================================");
        String password = "Su8023@bb";
        String encodedPassword = URLEncoder.encode(password, "UTF-8");
        String url = "ftp://ftpuser:"+encodedPassword+"@192.168.138.1:21/";
        FileObject fileObject = manager.resolveFile(url);
        FileObject[] children = fileObject.getChildren();
        for (FileObject child : children) {
            System.out.println("名称："+child.getName().getBaseName()+"---"+"类型:"+ child.getType().getName());

        }



        /**
         * 提供对 SFTP 服务器（即 SSH）上文件的访问 或 SCP 服务器）。
         *
         * URI 格式：sftp://[ username[: password]@] hostname[: port][ relative-path]
         * 例子：sftp://myusername:mypassword@somehost/pub/downloads/somefile.tgz
         * 默认情况下，该路径相对于用户的主目录。这可以通过以下方式进行更改：FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, false);
         */
        System.out.println("解析SFTP文件：=======================================");
/*        // 创建 JSch 会话配置对象并设置参数
        JSch jsch = new JSch();
        Session root = jsch.getSession("root", "192.168.138.100", 22);
        root.setPassword("centos");
        root.setConfig("StrictHostKeyChecking","no");
        root.connect();
        ChannelSftp sftp = (ChannelSftp) root.openChannel("sftp");
        Vector ls = sftp.ls("/root");*/

// 设置SFTP登录信息


        FileSystemOptions options = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options,true);
        SftpFileSystemConfigBuilder.getInstance().setIdentityProvider(options, JSch::removeAllIdentity);
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(options,"no");
        String sftppassword =URLEncoder.encode("centos","UTF-8");
        String remotePath = "sftp://root:"+sftppassword+"@192.168.138.100:22/";
        FileObject remoteDir = manager.resolveFile(remotePath, options);

        // List the children of the remote directory
        FileObject[] children2 = remoteDir.getChildren();
        for (FileObject child : children2) {
            System.out.println("名称："+child.getName().getBaseName()+"---"+"类型:"+ child.getType().getName());
            if (child.isFolder()){
                FileObject[] children3 = child.getChildren();
                for (FileObject subchild : children3) {
                    System.out.println("名称："+subchild.getName().getBaseName()+"---"+"类型："+ subchild.getType().getName());
                }
            }
        }


    }


    public static FileSystemOptions createSftpOptions(String username, String password) throws FileSystemException {
        FileSystemOptions options = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, false);
        SftpFileSystemConfigBuilder.getInstance().setTimeout(options, 10000);
//        SftpFileSystemConfigBuilder.getInstance().setProxyType(options, SftpFileSystemConfigBuilder.);
        SftpFileSystemConfigBuilder.getInstance().setProxyHost(options, "192.168.138.100:22");
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(options,"no");
        SftpFileSystemConfigBuilder.getInstance().setProxyPort(options, 22);
        SftpFileSystemConfigBuilder.getInstance().setProxyUser(options, "root");
        SftpFileSystemConfigBuilder.getInstance().setProxyPassword(options, "centos");
        return options;
    }
}
